/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BusPanel.java
 *
 * Created on Dec 19, 2011, 12:31:56 AM
 */
package otobustakipsistemi;

import DatabaseConnection.BusDbConnection;
import DatabaseConnection.PlaceDbConnection;
import DatabaseConnection.RouteDbConnection;
import DatabaseConnection.StopPointDbConnection;
import Entities.Bus;
import Entities.Route;
import Entities.StopPoint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author serhat
 */
public class BusPanel extends javax.swing.JPanel {

    private BusDbConnection busDbConnection;
    private PlaceDbConnection placeDbConnection;
    private DefaultTableModel placeTableModel;
    private RouteDbConnection routeDbConnection;
    private StopPointDbConnection stopPointDbConnection;

    /** Creates new form BusPanel */
    public BusPanel() {
        placeTableModel = new DefaultTableModel();
        initComponents();


    }

    private class PlaceListenerForEnableTable implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (addPlaka.getSelectedIndex() != -1 && start.getSelectedIndex() != -1
                    && end.getSelectedIndex() != -1) {
                placeTableModel.insertRow(0, new Object[]{start.getSelectedItem().toString(), "", ""});
                placeTableModel.insertRow(1, new Object[]{end.getSelectedItem().toString(), "", ""});
                addStopPoint.setEnabled(true);

            }
        }
    }

    public Vector<String> getPlakas() {
        try {
            busDbConnection = new BusDbConnection();
            ResultSet rs = busDbConnection.select(null, 1);
            Vector<String> plakas = new Vector<String>();
            while (rs.next()) {
                plakas.add(rs.getString(1));
            }
            return plakas;
        } catch (ClassNotFoundException ex) {
            errorMessage(ex.toString());
        } catch (SQLException ex) {
            errorMessage(ex.toString());
        } catch (InstantiationException ex) {
            errorMessage(ex.toString());
        } catch (IllegalAccessException ex) {
            errorMessage(ex.toString());
        }
        return null;
    }

    public Vector<String> getPlaces() {
        try {
            placeDbConnection = new PlaceDbConnection();
            ResultSet rs = placeDbConnection.select();
            Vector<String> places = new Vector<String>();
            while (rs.next()) {
                places.add(rs.getString(1));
            }
            return places;
        } catch (ClassNotFoundException ex) {
            errorMessage(ex.toString());
        } catch (SQLException ex) {
            errorMessage(ex.toString());
        } catch (InstantiationException ex) {
            errorMessage(ex.toString());
        } catch (IllegalAccessException ex) {
            errorMessage(ex.toString());
        }
        return null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        plaka = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        status = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        brand = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        year = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        model = new javax.swing.JTextField();
        save = new javax.swing.JButton();
        seats = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        addPlaka = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        start = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        end = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        placeTable = new javax.swing.JTable();
        place = new javax.swing.JComboBox();
        addStopPoint = new javax.swing.JButton();
        saveRoute = new javax.swing.JButton();

        jPanel1.setLayout(null);

        jLabel1.setText("Plaka");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(50, 10, 90, 30);
        jPanel1.add(plaka);
        plaka.setBounds(160, 10, 160, 30);

        jLabel2.setText("Koltuk Sayısı");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(50, 50, 82, 30);

        jLabel3.setText("Durum");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(50, 90, 82, 30);

        status.setText("Uygun");
        jPanel1.add(status);
        status.setBounds(160, 80, 160, 30);

        jLabel4.setText("Marka");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(50, 120, 82, 30);
        jPanel1.add(brand);
        brand.setBounds(160, 120, 160, 30);

        jLabel5.setText("Uretim Yılı");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(50, 160, 93, 30);

        year.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2006", "2007", "2008", "2009", "2010", "2011", "2012" }));
        seats.setSelectedIndex(-1);
        jPanel1.add(year);
        year.setBounds(160, 160, 160, 30);

        jLabel6.setText("Model");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(50, 200, 93, 30);
        jPanel1.add(model);
        model.setBounds(160, 200, 160, 30);

        save.setText("Kaydet");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        jPanel1.add(save);
        save.setBounds(160, 240, 160, 25);

        seats.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "46", "56" }));
        seats.setSelectedIndex(-1);
        jPanel1.add(seats);
        seats.setBounds(160, 50, 160, 30);

        jTabbedPane1.addTab("Otobüs Ekle", jPanel1);

        jLabel7.setText("Otobus Plakası");

        addPlaka.addActionListener(new PlaceListenerForEnableTable());
        addPlaka.setModel(new javax.swing.DefaultComboBoxModel(getPlakas()));
        addPlaka.setSelectedIndex(-1);

        jLabel8.setText("Kalkis");

        start.addActionListener(new PlaceListenerForEnableTable());
        start.setModel(new javax.swing.DefaultComboBoxModel(getPlaces()));
        start.setSelectedIndex(-1);

        jLabel9.setText("Varis");

        end.addActionListener(new PlaceListenerForEnableTable());
        end.setModel(new javax.swing.DefaultComboBoxModel(getPlaces()));
        end.setSelectedIndex(-1);

        jLabel10.setText("Duraklar");

        String[] tableColumnsName = {
            "Durak", "Tarih", "Saat"
        };
        placeTableModel.setColumnIdentifiers(tableColumnsName);
        placeTable.setModel(placeTableModel);
        jScrollPane1.setViewportView(placeTable);

        place.setModel(new javax.swing.DefaultComboBoxModel(getPlaces()));
        place.setSelectedIndex(-1);

        addStopPoint.setText("Ekle");
        addStopPoint.setEnabled(false);
        addStopPoint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addStopPointActionPerformed(evt);
            }
        });

        saveRoute.setText("Kaydet !");
        saveRoute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveRouteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addPlaka, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(end, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(place, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(addStopPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(410, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveRoute, javax.swing.GroupLayout.DEFAULT_SIZE, 736, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addPlaka, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(end, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(place, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addStopPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveRoute))
        );

        addStopPoint.getAccessibleContext().setAccessibleName("");

        jTabbedPane1.addTab("Otubüse Sefer Ekle", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 765, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void errorMessage(String message) {
        JOptionPane.showMessageDialog(this, message, "Hata", JOptionPane.ERROR_MESSAGE);
    }
private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
    try {
        busDbConnection = new BusDbConnection();
        Bus bus = new Bus();

        bus.setPlaka(plaka.getText());
        bus.setSeats(Integer.parseInt(seats.getSelectedItem().toString()));
        int st = 0;
        if (status.isSelected()) {
            st = 1;
        }
        bus.setStatus(st);
        bus.setBrand(brand.getText());
        bus.setYear(Integer.parseInt(year.getSelectedItem().toString()));
        bus.setModel(model.getText());

        boolean isOk = busDbConnection.insert(bus);

        if (isOk) {
            JOptionPane.showMessageDialog(this, "Otobus sisteme eklendi.", "Bilgi", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "Otobus sisteme eklenemedi !", "Hata", JOptionPane.ERROR_MESSAGE);
        }

    } catch (ClassNotFoundException ex) {
        errorMessage(ex.toString());
    } catch (SQLException ex) {
        errorMessage(ex.toString());
    } catch (InstantiationException ex) {
        errorMessage(ex.toString());
    } catch (IllegalAccessException ex) {
        errorMessage(ex.toString());
    }

}//GEN-LAST:event_saveActionPerformed

private void addStopPointActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addStopPointActionPerformed
    int row = placeTableModel.getRowCount() - 1;
    placeTableModel.insertRow(row, new Object[]{place.getSelectedItem().toString(), "", ""});
}//GEN-LAST:event_addStopPointActionPerformed

private void saveRouteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveRouteActionPerformed
    try {
        routeDbConnection = new RouteDbConnection();
        Route route = new Route(addPlaka.getSelectedItem().toString(), 
                start.getSelectedItem().toString(), 
                end.getSelectedItem().toString());
        int routeId = routeDbConnection.insert(route);
        System.out.println(routeId);
        if (routeId == -1){
            JOptionPane.showMessageDialog(this, "Sefer sisteme eklenemedi !", "Hata", JOptionPane.ERROR_MESSAGE);
            return;
        }
        stopPointDbConnection = new StopPointDbConnection();
        for (int i = 0; i < placeTableModel.getRowCount(); i++) {
            StopPoint stopPoint = new StopPoint();
            stopPoint.setRouteId(routeId);
            stopPoint.setName(placeTableModel.getValueAt(i, 0).toString());
            stopPoint.setDate(placeTableModel.getValueAt(i, 1).toString());
            stopPoint.setTime(placeTableModel.getValueAt(i, 2).toString());
            stopPointDbConnection.insert(stopPoint);
        }
        JOptionPane.showMessageDialog(this, "Sefer sisteme eklendi.", "Bilgi", JOptionPane.INFORMATION_MESSAGE);
        
    } catch (ClassNotFoundException ex) {
        errorMessage(ex.toString());
    } catch (SQLException ex) {
        errorMessage(ex.toString());
    } catch (InstantiationException ex) {
        errorMessage(ex.toString());
    } catch (IllegalAccessException ex) {
        errorMessage(ex.toString());
    }
}//GEN-LAST:event_saveRouteActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox addPlaka;
    private javax.swing.JButton addStopPoint;
    private javax.swing.JTextField brand;
    private javax.swing.JComboBox end;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField model;
    private javax.swing.JComboBox place;
    private javax.swing.JTable placeTable;
    private javax.swing.JTextField plaka;
    private javax.swing.JButton save;
    private javax.swing.JButton saveRoute;
    private javax.swing.JComboBox seats;
    private javax.swing.JComboBox start;
    private javax.swing.JCheckBox status;
    private javax.swing.JComboBox year;
    // End of variables declaration//GEN-END:variables
}
